r"""
 visualizations.py

 Tutt Library Systems Custom Visualizations using pygooglecharts

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 Copyright Jeremy Nelson, Colorado College
 """
__author__ = "Jeremy Nelson"
__version__ = "0.1.0"

import datetime,urllib
from models import Project

class GanettChartGenerator(object):
    """
    Class takes a project and generates a Ganett Chart
    """

    def __init__(self,**kwargs):
        if not kwargs.has_key('width'):
            self.width = 600
        else:
            self.width = kwargs.get('width')
        if not kwargs.has_key('height'):
            self.height = 200
        else:
            self.height = kwargs.get('height')
        if kwargs.has_key('projects'):
            self.projects = kwargs.get('projects')
            self.y_range = (0,len(self.projects))
        else:
            self.projects = []
        if kwargs.has_key('tasks'):
            self.tasks = kwargs.get('tasks')
            self.y_range = (0,len(self.tasks))
        else:
            self.tasks = []
        if kwargs.has_key('timespan'):
            x_range = kwargs.get('timespan')
#        else:
            # Creates a time-span of rolling five month period
            # with -1 from the current month to +4 months out
            

##        self.ganett_chart = StackedHorizontalBarChart(self.width,
##                                                      self.height,
##                                                      x_range=self.y_range)


class GoogleChart(object):
    google_url = 'http://chart.apis.google.com/chart?'
    google_codes = {'bhs':'Chart type',
                    'chco':'Row colors',
                    'chs':'Width and Height'}

    def __init__(self,**kwargs):
        if kwargs.has_key('size'):
            size = kwargs.get('size')

        else:
            # Default size 640 pixels by 480 pixels
            size = {'width':640,'height':480}
        self.chs = ['%sx%s' % (size['width'],
                               size['height'])]

    def get_url(self):
        pass

def create_xrange(timespan=None,days=[1,15,30]):
    output = '&chxl=0:'
    if not timespan:
        today = datetime.datetime.today()
        timespan = range(today.month -1,
                         today.month +4)
    counter = 0
    for month in timespan:
        for day in days:
            output += '|'
            time_display = '%02d/%02d' % (month,day)
            output += urllib.quote_plus(time_display)
            counter += 1
    return output

def create_yaxis(projects):
    output = '|1:'
    project_names = [project.name for project in projects]
    project_names.reverse()
    for name in project_names:
        output += '|%s' % urllib.quote_plus(name)
    return output


def generate_timelines(projects):
    google_url = 'http://chart.apis.google.com/chart?cht=bhs'
    google_url += '&chxt=x,y'
    google_url += '&chds=0,15'
    google_url += '&chs=600x200'
    google_url += '&chco=4d89f9'
    google_url += '&chtt=%s' % urllib.quote_plus('Tutt Library Systems Deadlines')
    google_url += '&chd=t:'
    for i,project in enumerate(projects):
        google_url += '%s,' % project.total_days()
    google_url += create_xrange()
    google_url += create_yaxis(projects)
    
    
    return google_url 
    
    
    
    
    
    
              
    

class GanettChart(object):


    def __init__(self,**kwargs):
        self.params = {'chdl':'|Complete|On Time|Late|Upcoming|Other',
                       'chds':[],
                       'chco':['4d89f9'],
                       'chs':'600x200',
                       'cht':'bhs',
                       'chtt':'Tutt Library System Projects Ganett Chart',
                       'chxl':'0:'}


    def create_xrange(self,timespan=None,days=[1,15,30]):
        if not timespan:
            today = datetime.datetime.today()
            timespan = range(today.month -1,
                             today.month +4)
        counter = 0
        for month in timespan:
            for day in days:
                self.params['chxl'] += '|%02d/%02d' % (month,day)
                counter += 1
        self.params['chds'] = [0,counter]


    def create_yrange(self,project_names=[]):
        self.params['chxl'] += '|1:'
        project_names.reverse()
        for name in projects_names:
            self.params['chxl'] += '|%s' % name
        
            
            



                                                      
        
            

